import { app, BrowserWindow, Menu, ipcMain } from 'electron'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow() {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    minWidth: 1440,
    minHeight: 900,
    width: 1440,
    height: 900,
    frame: false, // 无边框
    center: true,  // 应用居中
    // backgroundColor: '#010f25'
  })

  mainWindow.loadURL(winURL);

  // 菜单
  Menu.setApplicationMenu(null)

  // 开发者工具
  // mainWindow.webContents.toggleDevTools()

  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */


/*
*
*  最小化、最大化、关闭
*
*/
ipcMain.on('min', e => mainWindow.minimize());
ipcMain.on('max', (e, arg) => {
  if (mainWindow.isMaximized()) {
    // 复原
    mainWindow.unmaximize()
    e.sender.send('max-res', 0);
  } else {
    // 最大化
    mainWindow.maximize()
    e.sender.send('max-res', 1);
  }
});
ipcMain.on('close', e => mainWindow.close());